define(["jquery","bbcare"],function($,$bb){
	var servers = {};
	var URL = $bb.baseUrl("followapp");

	/**
	 * 查询患者基本信息
	 */
	servers.ajaxUserBaseInfo = function(params,callback){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/qryUserBaseInfo",
			data:JSON.stringify({
				data : {
					"userId":params.userId,
					"busiTypeList":params.busiTypeList,
					"showFlag":"1",
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询患者基本信息出错！");
				}	
			}
		});
	}

	/**
	 * 获取进度条里的className（患者列表数据处理里用到）
	 */
	var getProgressClassName = function(list){
		if(list && list.length > 0){
			for (var i = 0; i < list.length; i++) {
				var str = list[i];
				list[i] = {};
				if( str == '0' ){						// 未开始
					list[i].className = "progress-notstart";
					list[i].title = "未开始";
				}else if( str == '1' ){				// 已完成
					list[i].className = "progress-finish";
					list[i].title = "已完成";
				}else if( str == '2' ){				// 过期（未开始）
					list[i].className = "progress-overdue";
					list[i].title = "过期";
				}else if( str == '3' ){				// 过期（补填）
					list[i].className = "progress-fill";
					list[i].title = "补填";
				}else if( str == '4' ){				// 未完成（部分任务填写）
					list[i].className = "progress-nofinish";
					list[i].title = "未完成";
				}
			}
		}
		return list;
	}

	/**
	 * 患者列表数据处理
	 */
	servers.handleBaseInfo = function(obj){
		obj.width = 100 / obj.totalNodeNum + '%';
		obj.nodeStatusArr = getProgressClassName(obj.nodeStatusArr);
		if( !obj.hisNo ) obj.hisNo = obj.hisNo ? obj.hisNo : obj.hisNo1 ? obj.hisNo1 : obj.hisNo2 ? obj.hisNo2 : null;
		return obj;
	}

	/**
	 * 查询待审核任务
	 */
	servers.ajaxUserAuditTask = function(params,callback){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/qryAuditTask",
			data:JSON.stringify({
				data : {
					"userId":params.userId,
					"queueId":params.queueId,
					"taskStatus":"4"
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询待审核任务出错！");
				}	
			}
		});
	}

	/**
	 * 请求栏目模版
	 */
	servers.ajaxContentTempl = function(params,callback){
		var tenantId = $bb.getTenant().tenantId;
		$bb.ajax({
			url: URL + "contenttempl/getContentTempl",
			data:JSON.stringify({
				data : {
					"userId":params.userId,
					//"queueId":params.queueId,
					"tenantId":tenantId,
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data);
				}else{
					$bb.toastShow(data.msg ? data.msg : "获取栏目模版出错！");
				}	
			}
		});
	}

	/**
	 * 查询患者临床路径下的节点
	 */
	servers.ajaxUserCpNodes = function(params,callback){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/qryUserCpNodeList",
			data:JSON.stringify({
				data : {
					"userId":params.userId,
					"busiTypeList":params.busiTypeList,
					"showFlag":"1",
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询患者临床路径下的节点出错！");
				}	
			}
		});
	}

	/**
	 * 处理节点数据 为了按钮组
	 */
	servers.handleUserCpNodes = function(list,index){
		var colorArr = ["#E05EE1","#17DCC0","#00CEE0","#FF9300","#7AC62D"];
		for (var i = 0; i < list.length; i++) {
			if( i > 0 ) list[i].preId = list[i-1].id;
			if( i < list.length - 1 ) list[i].nextFollowDate = list[i+1].executeDate; // 下次随访日期
			list[i].index = i;
			list[i].color = colorArr[i%colorArr.length];
		}
		var M = 5;
		if( list.length <= M ) return list;
		var arr = [];
		if( index < M - 1 ){
			for(var i = 0; i < M; i++){
				arr.push(list[i]);
			}
		}else if( index == list.length - 1 ){
			for (var i = list.length - M; i < list.length; i++) {
				arr.push(list[i]);
			}
		}else{
			for (var i = index-(M-1-1); i <= (index + 1); i++) {
				arr.push(list[i]);
			}
		}
		return arr;
	}

	/**
	 * 处理节点数据
	 */
	servers.handleNodes = function(list){
		for (var i = 0; i < list.length; i++) {
			//list[i].index = $bb.simple.fillZero(list[i].seqNo + 1);
		}
		return {"list":list};
	}

	/**
	 * 获取默认当前索引号
	 */
	servers.getCurrentCp = function(list){
		for (var i = 0; i < list.length; i++) {
			if( list[i].isCurrentNode == "Y" ){
				return i;
			}
		}
	}

	/**
	 * 根据id获取当前索引号
	 */
	servers.getCurrentCpByTempNodeId = function(list,tempNodeId){
		for (var i = 0; i < list.length; i++) {
			if( list[i].id == tempNodeId ){
				return i;
			}
		}
	}

	/**
	 * 获取当前索引号
	 */
	servers.getCurrentIndex = function(list,index){
		for (var i = 0; i < list.length; i++) {
			if( list[i].index == index ){
				return i;
			}
		}
	}

	/**
	 * 处理时间数据
	 */
	servers.handleDates = function(data){
		if( data.recordTime ) data.recordTime = data.recordTime.substr(0,13) + "时";
		return data;
	}

	/**
	 * 查询节点下的任务
	 */
	servers.ajaxTasks = function(params,callback){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/qryUserCpTaskList",
			data:JSON.stringify({
				data : {
					"ptNodeId":params.id
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询节点下的任务出错！");
				}
			}
		});
	}

	/**
	 * 查询任务下的内容
	 */
	servers.ajaxCont = function(params,callback){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/qryUserCpTaskCont",
			data:JSON.stringify({
				data : {
					"ptTaskId":params.curTask.id
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询任务下的内容出错！");
				}
			}
		});
	}

	/**
	 * 疫苗默认值
	 */
	servers.getValueVaccine = function(params,valueList){
		var obj1 = {
			"deCode":$bb.code.getDeCodeByName("inoculationMonth"),
			"quesValue":params.curNode.nodeName
		};
		var obj2 = {
			"deCode":$bb.code.getDeCodeByName("vaccineName"),
			"quesValue":params.curTask.taskName
		};
		if( !valueList && valueList.length == 0 ){
			valueList = [obj1,obj2];
		}else{
			var flag1 = false, flag2 = false;
			for (var i = 0; i < valueList.length; i++) {
				if( valueList[i].deCode == obj1.deCode ) flag1 = true;
				if( valueList[i].deCode == obj2.deCode ) flag2 = true;
			}
			if( !flag1 ) valueList.push(obj1);
			if( !flag2 ) valueList.push(obj2);
		}
		return valueList;
	}

	/**
	 * 下次随访时间默认值
	 */
	servers.getValueNextFollowDate = function(valueList,date){
		if( date ){
			var deCode = $bb.code.getDeCodeNameByName("nextFollowDate");
			var flag = false;
			for (var i = 0; i < valueList.length; i++) {
				if( valueList[i].deCode == deCode ){
					flag = true;
					break;
				}
			}
			if( !flag ){
				var obj = { "deCode":deCode,"quesValue":date };
				valueList.push(obj);
			}
		}
		return valueList;
	}

	/**
	 * 填写或修改患者任务内容信息
	 */
	servers.ajaxSubmitTask = function(params1,callback,params){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/submitUserCpTask",
			data:JSON.stringify({
				data : params1
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "填写或修改患者任务内容信息出错！");
				}
			}
		});
	}

	/**
	 * 删除患者
	 */
	servers.ajaxUserDelete = function(params,callback){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/deleteQueueUser",
			data:JSON.stringify({
				data : params
			}),
			success: function(data){
				if(data.success){
					callback(data.data);
				}else{
					$bb.toastShow(data.msg ? data.msg : "删除患者出错！");
				}
			}
		});
	}

	/**
	 * 处理tasks
	 */
	servers.handleTasks = function(options){
		/**
		 * 2 门诊渠道 
		 * 4 微信公众号渠道 
		 * 5 诊间扫码渠道 
		 * 7 app渠道 
		 * 8 电子健康档案查询渠道
		 */
		for (var i = 0; i < options.list.length; i++) {
			if( options.list[i].showChannelTypeList && $.isArray(options.list[i].showChannelTypeList) && options.list[i].showChannelTypeList.length > 0 ){
				for (var j = 0; j < options.list[i].showChannelTypeList.length; j++) {
					if( options.list[i].showChannelTypeList[j] == "2" ){
						options.list[i].clinic = true;
					}else if( options.list[i].showChannelTypeList[j] == "4" ){
						options.list[i].vipcn = true;
					}else if( options.list[i].showChannelTypeList[j] == "5" ){
						options.list[i].ewm = true;
					}else if( options.list[i].showChannelTypeList[j] == "7" ){
						options.list[i].app = true;
					}else if( options.list[i].showChannelTypeList[j] == "8" ){
						options.list[i].ehr = true;
					}
				}
			}
			if( options.list[i].synchChannelTypeList && $.isArray(options.list[i].synchChannelTypeList) && options.list[i].synchChannelTypeList.length > 0 ){
				for (var j = 0; j < options.list[i].synchChannelTypeList.length; j++) {
				 	if( options.list[i].synchChannelTypeList[j] == "2" ){
				 		options.list[i].clinicLight = "active";
				 	}else if( options.list[i].synchChannelTypeList[j] == "4" ){
				 		options.list[i].vipcnLight = "active";
				 	}else if( options.list[i].synchChannelTypeList[j] == "5" ){
				 		options.list[i].ewmLight = "active";
				 	}else if( options.list[i].synchChannelTypeList[j] == "7" ){
				 		options.list[i].appLight = "active";
				 	}else if( options.list[i].synchChannelTypeList[j] == "8" ){
				 		options.list[i].ehrLight = "active";
				 	}
				} 
			}
		}
		return options;
	}

	/**
	 * 节点实例计划日期（即窗口期）修改
	 */
	servers.ajaxModifyPtNodeWinDate = function(params,callback,curNode){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/modifyPtNodeWinDate",
			data:JSON.stringify({
				data : params
			}),
			success: function(data){
				if(data.success){
					callback(data.data,curNode);
				}else{
					$bb.toastShow(data.msg ? data.msg : "修改计划日期出错！");
				}
			}
		});
	}

	/**
	 * 节点实例诊疗日期修改
	 */
	servers.ajaxModifyPtNodeAuthDiagDate = function(params,callback,curNode){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/modifyPtNodeAuthDiagDate",
			data:JSON.stringify({
				data : params
			}),
			success: function(data){
				if(data.success){
					callback(data.data,curNode);
				}else{
					$bb.toastShow(data.msg ? data.msg : "修改诊疗日期出错！");
				}
			}
		});
	}

	/**
	 * 是否存在临时随访节点模板
	 */
	servers.ajaxChkNewTempCpNode = function(params,callback){
		var params1 = $bb.getPageParams();
		$bb.ajax({
			url: URL + "ctrl/ptmanage/chkNewTempCpNode",
			data:JSON.stringify({
				data : {
					"ptNodeId":params.id,
					"busiTypeList":params1.busiTypeList,
					"subBusiTypeList":params1.subBusiTypeList,
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询是否存在临时随访节点模板出错！");
				}
			}
		});
	}

	/**
	 * 添加临时随访节点
	 */
	servers.ajaxAddTempPtCpNode = function(params,callback){
		$bb.ajax({
			url: URL + "ctrl/ptmanage/addTempPtCpNode",
			data:JSON.stringify({
				data : params
			}),
			success: function(data){
				if(data.success){
					callback(data.data);
				}else{
					$bb.toastShow(data.msg ? data.msg : "添加临时随访节点出错！");
				}
			}
		});
	}

	return servers;
});